package com.gitee.wsl.common.ui.ext.path

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Path
import com.gitee.wsl.math.Math.getDegree
import com.gitee.wsl.math.degreesToRadians
import kotlin.math.cos
import kotlin.math.sin

/**
 * 画线并添加圆点 （绘制的线是起始点到圆角结束点的路径，并不包含到p3点路径）
 *
 * @param startRadius 起始圆角半径( a边（p1 - p2）
 * @param endRadius   结束圆角半径( b边 (p2 - p3)
 * @param x1          起始点
 * @param y1
 * @param x2          中间点
 * @param y2
 * @param x3          结束点
 * @param y3
 * return b 边上 endRadius 点
 */
fun Path.lineToAndCorner(
    startRadius: Float,
    endRadius: Float = startRadius,
    x1: Float,
    y1: Float,
    x2: Float,
    y2: Float,
    x3: Float,
    y3: Float,
):Offset {
    val onePoint = getOnLinePointLocationEnd(startRadius, x1, y1, x2, y2)
    lineTo(onePoint)
    val twoPoint = getOnLinePointLocationStart(endRadius, x2, y2, x3, y3)
    //绘制圆角
    cubicTo(onePoint.x, onePoint.y, x2, y2, twoPoint.x, twoPoint.y)
    return twoPoint
}

/**
 * 获取线上点坐标
 *
 * @param lenght 线上点距离起始点(x1,y1）长度
 * @param x1     起始点x坐标
 * @param y1     起始点y坐标
 * @param x2     结束点x坐标
 * @param y2     结束点y坐标
 * @return
 */
private fun getOnLinePointLocationStart(
    lenght: Float,
    x1: Float,
    y1: Float,
    x2: Float,
    y2: Float,
): Offset {
    val degree = getDegree(x1, y1, x2, y2)
    val dx = getRightSideFromDegree(degree, lenght)
    val dy = getLeftSideFromDegree(degree, lenght)
    val v2 = x1 + dx
    val v3 = y1 + dy
    return Offset(v2.toFloat(), v3.toFloat())
}

/**
 * 获取线上点坐标
 *
 * @param lenght 线上点距离结束点（x2,y2）长度
 * @param x1     起始点x坐标
 * @param y1     起始点y坐标
 * @param x2     结束点x坐标
 * @param y2     结束点y坐标
 * @return
 */
private fun getOnLinePointLocationEnd(
    lenght: Float,
    x1: Float,
    y1: Float,
    x2: Float,
    y2: Float,
): Offset {
    val degree: Double = getDegree(x1, y1, x2, y2)
    val dx: Double = getRightSideFromDegree(degree, lenght)
    val dy: Double = getLeftSideFromDegree(degree, lenght)
    val v2 = x2 - dx
    val v3 = y2 - dy
    return Offset(v2.toFloat(), v3.toFloat())
}


/**
 * 直角三角形 根据角度和斜边求直角边
 *
 * @param degree 角度
 * @param width  斜边
 * @return 直角边长
 */
private fun getRightSideFromDegree(degree: Double, width: Float): Double {
    val cos = cos(degree.degreesToRadians())
    return width * cos
}

private fun getLeftSideFromDegree(degree: Double, width: Float): Double {
    val sin = sin(degree.degreesToRadians())
    return width * sin
}